<template>
  <div id="car">
    <div id="hood"></div>
    <div class="car1">
      <div class="spcar">
        <span :class="['ico-car',total<=0? '':'ico-car1']" @click="upp()" id="ico-car3"></span>
        <div class="spcar2" id="spcar2">
          <template v-if="total<=0">
            <span>购物车是空的</span>
          </template>
          <template v-else>
            <span style="color:red">￥{{total}}</span>
          </template>
        </div>
        <div class="spcar3">
          <a href :class="total<=0? '':'spa1'">去结算</a>
        </div>
      </div>
      <div class="addcar" id="caradd">
        <ul>
          <li class="addhead">
            <span class="sp1"></span>
            <p>
              <span class="sp2">全选</span>
              <span class="sp3">(已选{{totalnum}}件)</span>
            </p>
            <i @click="Eempty()">清空购物车</i>
          </li>
        </ul>
        <ul class="ul2">
          <li class="addbody" v-for="(goods,key,index) in cars[shopid]" :key="'goods'+index">
            <div class="bd">
              <span class="bd1"></span>
              <div class="bd2">
                <img :src="imgURL+goods.pics" alt>
                <div class="bd3">
                  <p class="p1">{{goods.title}}</p>
                  <i class="bdsp1">秒杀</i>
                  <p class="p2">
                    <span>￥{{goods.sprice}}</span>
                    <del>￥{{goods.price}}</del>
                  </p>
                </div>
                <div class="sec-btn">
                  <div>
                    <span class="btn1" @click="reduce(goods)"></span>
                    <span class="txt">{{goods.num}}</span>
                  </div>
                  <span class="btn2" @click="add(goods,index)"></span>
                </div>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="navigation" @click="changNav()">
      <div class="nav-box" v-show="flag">
        <router-link to="/" href class="a1">首页</router-link>
        <router-link to href class="a2">分类</router-link>
        <router-link to="/shoppingcar" href class="a3">购物车</router-link>
        <router-link to="/order" href class="a4">订单</router-link>
        <router-link to="/myself" href class="a5">我的</router-link>
        <i></i>
      </div>
    </div>
  </div>
</template>
<script>
import $ from "jquery";
export default {
  data() {
    return {
      leng: "",
      flag: false,
    };
  },
  props: ["shopid"],
  computed: {
    total() {
      const goods = this.cars[this.shopid];
      var totalprice = 0;
      for (let key in goods) {
        const temp = goods[key].sprice * goods[key].num;
        totalprice += temp;
      }
      return totalprice;
    },
    totalnum() {
      const goods = this.cars[this.shopid];
      var totalnums = 0;
      for (let key in goods) {
        const num = goods[key].num;
        totalnums += num;
      }
      return totalnums;
    }
  },
  methods: {
    changNav() {
      this.flag = !this.flag;
    },
    // 清空购物车
    Eempty() {
      const goods = this.cars[this.shopid];
      // 遍历goods中所有的数量让其为0 同时删除其中所有的goods
      Object.keys(goods).forEach(function(key) {
        goods[key].num = 0;
        delete goods[key];
      });
    this.getlength()
      $("#caradd,.ico-car").css({
        transform: "translateY(0px)",
        transition: "0s"
      });
      $("#hood").css({
        zIndex: "-10"
      });
      $("#spcar2").css({
        transform: "translateX(0px)",
        transition: "0s"
      });
    },
    // 添加商品
    add(goods, index) {
      if (goods.num < goods.store) {
        goods.num++;
      }
    },
    // 减少商品
    reduce(goods, index) {
      goods.num--;
      if (goods.num == 0) {
        this.$delete(this.cars[this.shopid], goods.id);
        this.getlength();
        if (this.leng == 0) {
          $("#hood").css({
            zIndex: "-10"
          });
        }
        $("#caradd,#ico-car3").css("transition", "0s");
        $("#spcar2").css({
          transform: "translateX(0px)",
          transition: "0s"
        });
      }
    },
    getlength() {
      let length = Object.keys(this.cars[this.shopid]).length;
      this.leng = length;
      let heigh1 = $(".addbody").height();
      let heigh2 = $("#spcar2").height();
      let heigh3 = $(".addhead").height();
      // 显示商品最多显示5个多了出现滚轮
      if (this.leng <= 0) {
        $("#caradd,.ico-car1").css({
          transform: "translateY(0px)",
           transition: "ease 1s"
        });
      } else if (this.leng <= 5 && this.totalnum > 0) {
        $("#caradd").css({
          transform:
            "translateY(" + (-this.leng * heigh1 - heigh2 - heigh3) + "px)",
          transition: " 1s"
        });
        $(".ico-car1").css({
          transform:
            "translateY(" + (-this.leng * heigh1 - heigh2 - heigh3 + 8) + "px)",
           transition: " 1s"
        });
      } else if (this.totalnum > 0) {
        $("#caradd").css({
          transform: "translateY(" + (-5 * heigh1 - heigh2 - heigh3) + "px)",
          transition: " 1s"
        });
        $(".ico-car1").css({
          transform:
            "translateY(" + (-5 * heigh1 - heigh2 - heigh3 + 8) + "px)",
          transition: " 1s"
        });
      }
    },
    upp() {
      this.getlength();
      if (this.leng > 0 && this.totalnum > 0) {
        $("#spcar2").css({
          transform: "translateX(-60px)",
          transition: " 1s"
        });
        $("#hood").css({
          zIndex: "0"
        });
        if ($(".ico-car1").position().top < -30) {
          $("#caradd,.ico-car1").css({
            transform: "translateY(0px)",
            transition: " 1s"
          });
          $("#spcar2").css({
            transform: "translateX(0px)",
            transition: " 1s"
          });
          $("#hood").css({
            zIndex: "-10"
          });
        }
      }
    }
  }
};
</script>
<style lang='scss' scoped>
* {
  margin: 0;
  padding: 0;
  font-style: normal;
}
li {
  list-style: none;
}
a {
  text-decoration: none;
}
#car {
  width: 100%;
  #hood {
    position: fixed;
    right: 0px;
    left: 0px;
    top: 0px;
    bottom: 0px;
    background: rgba(0, 0, 0, 0.4);
    z-index: -10;
  }
  .car1 {
    position: fixed;
    bottom: 0px;
  }
  .spcar {
    font-size: 28px;
    width: 750px;
    background-color: #fff;
    display: flex;
    position: relative;
    .ico-car {
      display: block;
      width: 160px;
      height: 118px;
      background: url(../image/shopdetails/cart.png) 10px 10px no-repeat;
      background-size: 100px 250px;
      position: absolute;
      z-index: 1;
      bottom: 10px;
    }
    .ico-car1 {
      background: url(../image/shopdetails/cart.png) 10px -140px no-repeat;
      background-size: 100px 250px;
    }
    .spcar2 {
      width: 440px;
      padding-left: 160px;
      background-color: #fff;
      span {
        display: inline-block;
        height: 100px;
        line-height: 100px;
        width: auto;
        text-align: left;
        color: #ccc;
      }
    }
    .spcar3 {
      width: 150px;
      height: 100px;
      a {
        display: block;
        width: 150px;
        text-align: center;
        line-height: 100px;
        color: #fff;
        background-color: #ccc;
        font-weight: 600;
      }
      .spa1 {
        background-color: rgba(0, 190, 52, 1);
      }
    }
  }

  .addcar {
    position: absolute;
    z-index: -1;
    font-size: 24px;
    width: 750px;
    color: #666;
    background: #fff;
    .addhead {
      border-bottom: 1px solid #eee;
      height: 80px;
      padding: 0px 20px;
      span {
        display: inline-block;
      }
      .sp1 {
        float: left;
        width: 80px;
        height: 80px;
        background: url(../image/shopdetails/checked1.png) no-repeat center;
        background-size: 40px 38px;
      }
      p {
        line-height: 80px;
        float: left;
      }
      i {
        color: #666;
        display: block;
        float: right;
        padding: 0 20px 0px 60px;
        height: 80px;
        line-height: 80px;
        background: url(../image/shopdetails/delete.png) no-repeat 20px center;
        background-size: 30px 30px;
      }
    }
    .ul2 {
      max-height: 850px;
      overflow-y: scroll;
      .addbody {
        .bd {
          padding: 20px 20px;
          display: flex;
          position: relative;
          .bd1 {
            display: block;
            width: 80px;
            height: 80px;
            background: url(../image/shopdetails/checked1.png) no-repeat center;
            background-size: 40px 38px;
          }
          .bd2 {
            display: flex;

            img {
              width: 130px;
              height: 130px;
            }
            .bd3 {
              padding-left: 20px;
              .p1 {
                width: 400px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                padding-right: 10px;
                font-size: 30px;
                color: #333;
                padding-bottom: 10px;
              }
              .bdsp1 {
                display: inline-block;
                background-color: #ff5138;
                border: 1px solid #ff5138;
                border-radius: 10px 0px;
                color: #fff;
                padding: 5px;
              }
              .p2 {
                margin-top: 10px;
                font-size: 28px;
                span {
                  color: #ff5138;
                }
              }
            }
            .sec-btn {
              position: absolute;
              right: 30px;
              bottom: 20px;
              display: flex;
              span {
                display: block;
                width: 56px;
                height: 56px;
                line-height: 56px;
                text-align: center;
                font-size: 30px;
              }
              div {
                display: flex;
                position: absolute;
                right: 70px;
                .btn1 {
                  background: url(../image/shopdetails/addNumSprite.png)
                    no-repeat;
                  background-position: -85px 0;
                  background-size: 224px 57px;
                  margin-right: 10px;
                }
              }
              .btn2 {
                background: url(../image/shopdetails/addNumSprite.png);
                background-position: 0 0;
                background-size: 224px 57px;
              }
            }
          }
        }
      }
    }
  }
  .navigation:hover .nav-box {
    display: block;
  }
  .navigation {
    position: fixed;
    right: 0px;
    bottom: 400px;
    width: 72px;
    height: 72px;
    background: rgba(0, 0, 0, 0.8) url(../image/shopdetails/bm.png) 14px 14px
      no-repeat;
    background-size: 130px 40px;
    border-radius: 10px 0px 0px 10px;
    .nav-box {
      width: 190px;
      background-color: rgba(0, 0, 0, 0.8);
      position: absolute;
      bottom: 80px;
      right: 0px;
      //z-index: 3;
      display: none;

      a {
        display: block;
        color: #fff;
        padding-left: 76px;
        border-bottom: 1px solid #4b4b4b;
        line-height: 70px;
        background: url(../image/shopdetails/bmenu.png) no-repeat;
        font-size: 28px;
        background-size: 30px 600px;
      }
      .a1 {
        background-position: 20px -100px;
      }
      .a2 {
        background-position: 20px -200px;
      }
      .a3 {
        background-position: 20px -300px;
      }
      .a4 {
        background-position: 20px -397px;
      }
      .a5 {
        background-position: 20px -500px;
      }
      i {
        width: 0;
        height: 0;
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-top: 10px solid #4b4b4b;
        position: absolute;
        right: 30px;
      }
    }
  }
}
</style>

